package com.study.service.impl;

import com.study.dao.IAccountDao;
import com.study.domain.Account;
import com.study.service.IAccountService;
import com.study.utils.TransactionUtil;

import java.util.List;

/**
 * 账户的业务层实现类
 * 事务控制应该都在业务层,而不是持久层
 */
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao;
    private TransactionUtil transactionUtil;

    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void setTransactionUtil(TransactionUtil transactionUtil) {

        this.transactionUtil = transactionUtil;
    }

    @Override
    public List<Account> findAllAccount() {

        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {

        return accountDao.findAccountById(accountId);
    }

    @Override
    public void saveAccount(Account account) {

        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {

        accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccount(Integer acccountId) {

        accountDao.deleteAccount(acccountId);
    }

    @Override
    public void tranferAccount(int sourceid, int targetid, Float money) {

            Account source = accountDao.findAccountById(sourceid);
            Account target = accountDao.findAccountById(targetid);
            source.setMoney(source.getMoney() - money);
            target.setMoney(target.getMoney() + money);
            accountDao.updateAccount(source);
            accountDao.updateAccount(target);
    }
}
